﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using CoreShop.Model;
using CoreShop.Repository;
using SqlSugar;
namespace CoreShop.Services
{
    /// <summary>
    ///  接口实现
    /// </summary>
    public class corecmsfoodorderingtaskServices : Repository<corecmsfoodorderingtask>, IcorecmsfoodorderingtaskServices
    {
        public bool InsertAsync(corecmsfoodorderingtask entity)
        {
            return base.Change<corecmsfoodorderingtask>().Insert(entity);
        }
        public bool InsertOrUpdate(List<corecmsfoodorderingtask> entity)
        {
            return base.InsertOrUpdate(entity);
        }
        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(corecmsfoodorderingtask entity)
        {
            return base.Change<corecmsfoodorderingtask>().Update(a => new corecmsfoodorderingtask()
            {
                detailId = entity.detailId,
                tastId = entity.tastId
            }, a => a.id == entity.id);
        }

        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(List<corecmsfoodorderingtask> entity)
        {
            bool returnKey = true;
            foreach (var item in entity)
            {
                returnKey = base.Change<corecmsfoodorderingtask>().Update(a => new corecmsfoodorderingtask()
                {
                    detailId = item.detailId,
                    tastId = item.tastId,
                }, a => a.id == item.id);
            }
            return returnKey;
        }

        /// <summary>
        /// 重写删除指定ID的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteByIdAsync(long id)
        {
            return base.Change<corecmsfoodorderingtask>().DeleteById(id);
        }

        /// <summary>
        /// 重写删除指定ID集合的数据(批量删除)
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteByIdsAsync(int[] ids)
        {
            bool returnKey = true;
            foreach (int id in ids)
            {
                returnKey = base.Change<corecmsfoodorderingtask>().DeleteById(id);
            }
            return returnKey;
        }

        /// <summary>
        /// 根据商品类型ID查询类型名称
        /// </summary>
        /// <param name="custoType"></param>
        /// <returns></returns>
        public List<corecmsfoodorderingtask> QueryByOrderId(long id)
        {
            var orderings = base.Change<corecmsfoodorderingtask>().GetList(a => a.orderId == id).OrderBy(p => p.id).ToList();
            return orderings;
        }

        #region 获取缓存的所有数据==========================================================

        /// <summary>
        /// 获取缓存的所有数据
        /// </summary>
        /// <returns></returns>
        public List<corecmsfoodorderingtask> GetCaChe()
        {
            return base.GetList();
        }

        #endregion

        #region 根据条件查询分页数据
        /// <summary>
        /// 根据条件查询分页数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsfoodorderingtask> QueryPageAsync(ref int count, int? pageIndex, int? pageSize, string sName)
        {
            List<corecmsfoodorderingtask> custos = new List<corecmsfoodorderingtask>();
            var where = Expressionable.Create<corecmsfoodorderingtask>();
            if (pageIndex >= 1 && pageSize > 0)
            {
                custos = base.AsQueryable().WhereIF(true, where.ToExpression())
                    .ToPageList((int)pageIndex, (int)pageSize, ref count)
                    .OrderBy(a => a.id).ToList();
            }
            else
            {
                custos = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.id).ToList();
            }
            return custos;
        }
        #endregion

        #region 计算做法金额
        /// <summary>
        /// 保存套菜主商品
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public decimal GetOrderingTaskAmount(corecmsfoodordering entity)
        {
            decimal taskAmount = 0;
            var orderingTasks = base.AsQueryable().Where(p=>p.orderId == entity.orderId).ToList();
            if ((orderingTasks == null) || (orderingTasks.Count == 0)) { return 0; }
            foreach (var foodtask in orderingTasks)
            {
                //计算加价
                if (foodtask.addPriceWay == (int)Library.GlobalEnumVars.AddPriceWay.FixAdd)
                    taskAmount += foodtask.taskPrice;
                else if (foodtask.addPriceWay == (int)Library.GlobalEnumVars.AddPriceWay.QtyAdd)
                {
                    taskAmount += foodtask.taskPrice * entity.quantity;
                }
            }
            return taskAmount;
        }
        #endregion
    }
}
